---@class DarkStreetPlayerItem : DSActorItem
local DarkStreetPlayerItem = Class("DarkStreetPlayerItem",DSActorItem)
local ATTR_SORT = {
    EnumConst.FighterAttributeEnum.ATK_PER,
    EnumConst.FighterAttributeEnum.HP_PER,
    EnumConst.FighterAttributeEnum.SPEED_PER,
}

function DarkStreetPlayerItem:OnCreate(obj)
    DSActorItem.OnCreate(self,obj)
    self.totalCount = dataTable.getGlobalInt(344)
    self.nameText = self:GetChild("Text_PlayerName")
    self.emoji = self:GetChild("Loader_emoji")
    self.rewardIcon = self:GetChild("Loader_Reward")
    self.rewardNumText = self:GetChild("Text_RewardNum")

    ---@type GProgressBar
    self.bloodBar = self:GetChild("Bar_BloodBar")
    self.ctrlHpColor = self:GetController("type",self.bloodBar)
    self.rewardChangeText = self:GetChild("Text_RewardChange")
    self.winBuffText = self:GetChild("Text_RankChange")
    self.ctrlEmoji = self:GetController("emoji")
    self.ctrlReward = self:GetController("reward")
    self.ctrlBattle = self:GetController("battle")
    self.ctrlWinBuff = self:GetController("buff")
    self.ctrlBuff = self:GetController("AddBuff")
    self.effRewardChange = self:GetTransition("Effect_RewarChange")
    self.graphModel = self:GetChild("Graph_model")
    self.List_AddBuff = self:GetList("List_AddBuff")--头顶的buff
    self:SetDynamicList(self.List_AddBuff,self.BuffRender,self,true)
    self.buffList = {}  --头顶的buff
    self.ctrlBuff.selectedIndex = 0
    self.List_AddBuff.numItems = 0
end
---@param player RedRob.Player
function DarkStreetPlayerItem:Refresh(player)
    local isMe = player == RedRob.KeyController.GetCtrl()
    if isMe then
        self.fguiObj.displayObject.gameObject.name = "me"
    else
        self.fguiObj.displayObject.gameObject.name = tostring(player:GetId())
    end
    self.ctrlHpColor.selectedIndex = isMe and 0 or 1
    if isMe then
        self.nameText.text = string.TextColor(player:GetName(),"#92cfff")
    else
        self.nameText.text = player:GetName()
    end
    local inBattle = player:IsInBattle()
    self:SetInBattle(inBattle)
    self:SetHp(player:GetHp(),player:GetHpMax())
    local data = RedRob.GameMode.GetPlayerData(player:GetId())
    local stage = RedRob.ClientBattle.GetBattleStage()
    if stage == RedRob.Config.BattleStage.battle then
        if data then
            self:ShowReward(not inBattle,data.resNum)
            self:ShowWinPunishBuff(data.buff)
        else
            self:ShowReward(not inBattle,self.totalCount)
            self:ShowWinPunishBuff(0)
        end
    else
        self:ShowReward(false)
        self:ShowWinPunishBuff(0)
    end
end
--100位制
function DarkStreetPlayerItem:ShowWinPunishBuff(buff)
    if buff == 0 then
        self.ctrlWinBuff.selectedIndex = 0
    elseif buff > 0 then
        self.ctrlWinBuff.selectedIndex = 1
        self.winBuffText.text = buff.."%"
    else
        self.ctrlWinBuff.selectedIndex = 2
        self.winBuffText.text = math.abs(buff).."%"
    end
end
---@param playerBuffList RedRob.Buffer[]
function DarkStreetPlayerItem:ShowHeadBuff(playerBuffList)
    if not playerBuffList then return end
    table.clear(self.buffList)
    for k,v in pairs(playerBuffList) do
        local cfg = dataTable.getDataTableByStringId("t_buff",v.buffId)
        if cfg and not string.IsNilOrEmpty(cfg.f_Atribute) then
            table.insert(self.buffList,v)
        end
    end
    table.sort(self.buffList,DarkStreetPlayerItem.SortBuff)
    self.ctrlBuff.selectedIndex = #self.buffList > 0 and 1 or 0
    self.List_AddBuff.numItems = #self.buffList
end
function DarkStreetPlayerItem.SortBuff(a,b)
    local aCfg = dataTable.getDataTableByStringId("t_buff",a.buffId)
    local bCfg = dataTable.getDataTableByStringId("t_buff",b.buffId)
    local aAttrId = aCfg.f_Atribute[1][1]
    local bAttrId = bCfg.f_Atribute[1][1]
    local aw = ATTR_SORT[aAttrId] or 0
    local bw = ATTR_SORT[bAttrId] or 0
    return aw < bw
end

function DarkStreetPlayerItem:BuffRender(index,obj)
    local buff = self.buffList[index]
    local icon = obj:GetChild("Loader_Buff")
    local numText = obj:GetChild("Text_BuffNum")
    local cfg = dataTable.getDataTableByStringId("t_buffshow",buff.buffId)
    if cfg then
        icon.url = EnumConst.AtlasPath.SkillAtlas..cfg.f_BuffIcon
        numText.text = tostring(buff.overlay)
    else
        logError("找不到t_buffshow"..buff.buffId)
    end
end
function DarkStreetPlayerItem:SetInBattle(isInBattle)
    self.ctrlBattle.selectedIndex = isInBattle and 1 or 0
end
function DarkStreetPlayerItem:SetRewardCount(count)
    if count < 0 then
        count = 0
    end
    self.rewardNumText.text = count
    self.totalCount = count
end
function DarkStreetPlayerItem:ShowReward(show,totalCount)
    self.ctrlReward.selectedIndex = show and 1 or 0
    if show then
        self:SetRewardCount(totalCount)
    end
end
function DarkStreetPlayerItem:SetHp(hp,max)
    self.bloodBar.value = hp/max * 100
end
function DarkStreetPlayerItem:HpBarVisible(visible)
    self.bloodBar.visible = visible
end
function DarkStreetPlayerItem:ShowChangeReward(num)
    if self.totalCount == 0 then return end
    self.ctrlReward.selectedIndex = 2
    if num >= 0 then
        self.rewardChangeText.text = "+"..num
    else
        self.rewardChangeText.text = tostring(num)
    end
end
function DarkStreetPlayerItem:ShowEmoji(emoji)
    self.emoji.url = "ui://Emoji/" .. emoji
    self.ctrlEmoji.selectedIndex = 1
    self.emojiTimerId = self:TimeCall(self.DelayHideEmoji,2000)
end
function DarkStreetPlayerItem:DelayHideEmoji()
    self.ctrlEmoji.selectedIndex = 0
end
return DarkStreetPlayerItem